﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    public class AdminService : LIMS.IDAL.IAdmin
    {
        LIMSDataContext db = new LIMSDataContext();

        /// <summary>
        /// 根据指定管理员ID获得该管理员姓名 
        /// </summary>
        /// <param name="adminID">指定的管理员ID</param>
        /// <returns>该管理员姓名</returns>
        public String FindName(String adminID)
        {
            return db.TB_admin.Single(sg => sg.mesAdminId == adminID).adminName;
        }

        /// <summary>
        /// 检测注册ID是否可用；返回true表示该ID可以使用
        /// </summary>
        /// <param name="id">待检测的用户ID</param>
        /// <returns></returns>
        public Boolean CheckID(String id)
        {
            return db.TB_admin.Where(s => s.mesAdminId == id).Select(s => new { s.mesAdminId }).Count() < 1;
        }

        public Boolean InsertAdmin(LIMS.Model.Admins admin)
        {

            TB_admin adminEntity = new TB_admin()
            {
                adminName = admin.adminName,
                adminPwd = admin.adminPwd,
                mesAdminId = admin.adminID
            };
            db.TB_admin.InsertOnSubmit(adminEntity);
            db.SubmitChanges();
            return true;
        }

        public List<LIMS.Model.Admins> FindAll()
        {
            List<LIMS.Model.Admins> adminTmp = new List<LIMS.Model.Admins>();
            var Demo = db.TB_admin.Select(s => s);
            foreach (var item in Demo)
            {
                LIMS.Model.Admins admin = new LIMS.Model.Admins()
                {
                    adminID = item.mesAdminId,
                    adminName = item.adminName,
                    adminPwd = item.adminPwd
                };
                adminTmp.Add(admin);
            }

            return adminTmp;
        }

        public LIMS.Model.Admins FindByID(String adminID)
        {
            Model.Admins admin = new Model.Admins();
            var adminTmp = db.TB_admin.Single(sg => sg.mesAdminId == adminID);
            if (adminID != null)
            {
                admin.adminID = adminTmp.mesAdminId;
                admin.adminName = adminTmp.adminName;
                admin.adminPwd = adminTmp.adminPwd;
            }
            return admin;
        }

        public List<LIMS.Model.Admins> FindByName(String name)
        {
            List<Model.Admins> admins = new List<Model.Admins>();
            db.TB_admin.Where(w => w.adminName.Contains(name)).Select(s => s).ToList().ForEach((item) =>
            {
                Model.Admins admin = new Model.Admins()
                {
                    adminID = item.mesAdminId,
                    adminName = item.adminName,
                    adminPwd = item.adminPwd
                };
                admins.Add(admin);
            });
            return admins;
        }


    }
}
